package com.hariro.customview.pulltofresh;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;

/**
 * BorderScrollView
 * <ul>
 * <li>onTop and onBottom response ScrollView</li>
 * <li>you can {@link #setOnBorderListener(OnBorderListener)} to set your top
 * and bottom response</li>
 * </ul>
 * 
 * @author trinea@trinea.cn 2013-5-21
 */
public class BorderScrollView extends ScrollView {

	private OnBorderListener onBorderListener;
	private View contentView;

	public BorderScrollView(Context context) {
		super(context);
	}

	public BorderScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public BorderScrollView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onScrollChanged(int x, int y, int oldx, int oldy) {
		super.onScrollChanged(x, y, oldx, oldy);
		doOnBorderListener();
	}

	public void setOnBorderListener(final OnBorderListener onBorderListener) {
		this.onBorderListener = onBorderListener;
		if (onBorderListener == null) {
			return;
		}

		if (contentView == null) {
			contentView = getChildAt(0);
		}
	}

	/**
	 * OnBorderListener, Called when scroll to top or bottom
	 * 
	 * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a>
	 *         2013-5-22
	 */
	public static interface OnBorderListener {

		/**
		 * Called when scroll to bottom
		 */
		public void onBottom();

		/**
		 * Called when scroll to top
		 */
		public void onTop();
	}

	private void doOnBorderListener() {
		if (contentView != null
				&& contentView.getMeasuredHeight() <= getScrollY()
						+ getHeight()) {
			if (onBorderListener != null) {
				onBorderListener.onBottom();
			}
		} else if (getScrollY() == 0) {
			if (onBorderListener != null) {
				onBorderListener.onTop();
			}
		}
	}
}
